<?php include_once "config/config.php"; session_start(); if (isset($_REQUEST['_SESSION'])) die("Get lost Muppet!");
if (!isset($_SESSION['stalas'])) {header('location:index.php');}
?>
<div>
    <?php
        $id = $_SESSION['stalas'];
        $result = mysql_query("SELECT Vardas FROM vartotojai WHERE id='$id'");
        $row = mysql_fetch_array($result);
        $date = date('Y-m-d');
        echo('<p style="float:right; font-size:0.8em">'.$date.'</p><br /><br />');
        echo('<span class="h2">Laba diena, '.$row['Vardas'].'.</span>');
    ?>
    <div class="separator">&nbsp;</div>
    <table border="0" cellspacing="0" width="90%">
        <colgroup>
            <col width="70%" />
            <col width="30%" />
        </colgroup>
        <tr class="header_bg">
            <td style="border-right: solid 1px;"><span>Skola: </span></td>
            <?php
                $result = mysql_query("SELECT Suma, Data, id FROM saskaitos WHERE VartotojaiID='$id' ORDER BY Data DESC");
                $result2 = mysql_query("SELECT Suma, Data FROM inestipinigai WHERE VartotojaiId='$id' ORDER BY Data DESC");
                
                $saskaitos = array();
                $j = 0;
                $skola = 0; $moketi = 0; $sumoketa = 0;
                while($row = mysql_fetch_array($result)) {
                    $saskaitos[$j]['suma'] = $row['Suma'];
                    $saskaitos[$j]['data'] = $row['Data'];
                    $saskaitos[$j]['id'] = $row['id'];
                    $moketi += $saskaitos[$j]['suma']; 
                    $j++;
                    
                }
                $apmokejimai = array();
                $j = 0;
                while($row = mysql_fetch_array($result2)) {
                    $apmokejimai[$j]['suma'] = $row['Suma'];
                    $apmokejimai[$j]['data'] = $row['Data'];
                    $sumoketa += $apmokejimai[$j]['suma'];
                    $j++;
                    
                }
                
                /*$result3 = mysql_query("SELECT * FROM VartotojoSkola WHERE VartotojaiId='$id'");
                $skola = 0;
                while($row = mysql_fetch_array($result3)) {
                    $skola += $row['NaujaSkola'] ;   
                } */
                $skola = $sumoketa - $moketi; 
                
                echo('<td style="padding-left: 30px;"><span>'.$skola.' Lt </span></td>');
            ?>
            
            
        </tr>
    </table>
    <table border="0" cellspacing="0" width="90%" class="lentele">
        <colgroup>
            <col width="30%" />
            <col width="40%" />
            <col width="10%" />
            <col width="20%" />
        </colgroup>
        <tr>
            <td class="theader" style="border-right-style:solid;"><span>Data</span></td>
            <td class="theader" style="padding-left:20px; border-right-style:solid;"><span>Pavadinimas</span></td>
            <td colspan="2" class="theader" style="padding-left: 20px;"><span>Suma</span></td>
        </tr>
        <tbody>
            <tr>
                <?php

                    $color="1";
                    $k = 0;

                    for ($j = 0; $j < count($saskaitos); $j++) {
                        $date = $saskaitos[$j]['data'];
                        $data = is_string($date) ? strtotime($date) : (is_int($date) ? $date : time());
                        $y = date('Y m d', $data);
                        
                        //$y2 = ''; 
                        if (isset($apmokejimai[$j]['data'])) {
                            $date2 = $apmokejimai[$j]['data'];
                            $data2 = is_string($date2) ? strtotime($date2) : (is_int($date2) ? $date2 : time());
                            $y2 = date('Y m d', $data2);
                            if($color == 1) {
                                echo('<tr class="balta">');
                                $color = "2";
                            }
                            else{
                                echo('<tr class="melyna">');
                                $color = "1";
                            }
                            echo('<td style="border-right: solid 1px;">'.$y2.'</td><td style="padding-left: 20px; border-right: solid 1px;">Mokėjimas </td><td style="float:right;">'.$apmokejimai[$j]['suma'].'</td><td> Lt</td></tr>');
                        }
                        
                        if($color == 1) {                                            
                            echo('<tr class="balta">');
                            $color = "2";
                        }
                        else{
                          echo('<tr class="melyna">');
                          $color = "1";
                        }
                        
                        if($j < count($saskaitos)-1 && $saskaitos[$j]['data'] == $saskaitos[$j+1]['data'] && $k != $j){
                            $saskaitos[$j]['suma'] += $saskaitos[$j+1]['suma'];
                            $k = $j+1;
                        }
                        if($j != $k || $k == 0){
                            echo('<td style="border-right: solid 1px;">'.$y.'</td><td style="padding-left: 20px; border-right: solid 1px;">Sąskaita '.$saskaitos[$j]['id'].'</td><td style="float:right;">'.$saskaitos[$j]['suma'].'</td><td> Lt</td></tr>');
                        }
                    }
                ?>
            </tr>
        </tbody>
    </table>
    <div style='clear:both'></div>
</div>